package cn.com.elite;

import cn.com.callback.EliteThreadInvoke;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @projectName: ngsapitask-sw
 * @package: cn.com.elite
 * @className: EliteThread
 * @author: LiJT
 * @description: TODO
 * @date: 2022-10-29 02:06
 * @version: 1.0
 */

public class EliteThread extends Thread {
    private static final Logger log = LoggerFactory.getLogger(EliteThread.class);
    private Class groovyClass;
    private EliteBean eliteBean;
    private String thread_guid;
    private String content;
    private String dataSourceID;

    public EliteThread(Class groovyClass1, EliteBean eliteBean1, String thread_guid1, String content1, String dataSourceID1) {
        this.groovyClass = groovyClass1;
        this.eliteBean = eliteBean1;
        this.thread_guid = thread_guid1;
        this.content = content1;
        this.dataSourceID = dataSourceID1;
    }

    public void run() {
        String threadName = Thread.currentThread().getName();
        log.debug("[" + this.thread_guid + "]进入线程:" + threadName);

        try {
            EliteThreadInvoke eliteThreadInvoke = (EliteThreadInvoke) this.groovyClass.newInstance();
            String sql = "update EliteThread set status='1' where thread_guid='" + this.thread_guid + "'";
            int sqlResult = this.eliteBean.update(sql, this.dataSourceID);
            log.debug("[" + this.thread_guid + "]线程:" + threadName + ":" + sql + ":" + sqlResult);

            int returnReslut = eliteThreadInvoke.doCallBack(this.eliteBean, this.content);

            if (returnReslut == 1) {
                sql = "update EliteThread set status='2' where thread_guid='" + this.thread_guid + "'";
                sqlResult = this.eliteBean.update(sql, this.dataSourceID);
                log.debug("[" + this.thread_guid + "]线程:" + threadName + ":" + sql + ":" + sqlResult);
            } else if (returnReslut == 0) {
                sql = "update EliteThread set status='-1' where thread_guid='" + this.thread_guid + "'";
                sqlResult = this.eliteBean.update(sql, this.dataSourceID);
                log.debug("[" + this.thread_guid + "]线程:" + threadName + ":" + sql + ":" + sqlResult);
            }
        } catch (Exception e) {
            log.error("[" + this.thread_guid + "]线程执行-异常!", e);
        }

        log.debug("[" + this.thread_guid + "]线程" + threadName + "执行完毕");
    }
}